package com.xlq.consultant.service.impl;


import com.xlq.consultant.mapper.ReservationDao;
import com.xlq.consultant.entity.Reservation;
import com.xlq.consultant.service.ReservationService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;


/**
 * (Reservation)表服务实现类
 *
 * @author makejava
 * @since 2025-06-29 22:53:36
 */
@Slf4j
@Service("reservationService")
public class ReservationServiceImpl implements ReservationService {
    @Resource
    private ReservationDao reservationDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Reservation queryById(Long id) {
        return this.reservationDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param reservation 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<Reservation> queryByPage(Reservation reservation, PageRequest pageRequest) {
        long total = this.reservationDao.count(reservation);
        return new PageImpl<>(this.reservationDao.queryAllByLimit(reservation, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param reservation 实例对象
     * @return 实例对象
     */
    //1.添加预约信息的方法
    @Override
    public int insert(Reservation reservation) {
        log.info("insert:{}",reservation.toString());
        return this.reservationDao.insert(reservation);
    }

    /**
     * 修改数据
     *
     * @param reservation 实例对象
     * @return 实例对象
     */
    @Override
    public Reservation update(Reservation reservation) {
        this.reservationDao.update(reservation);
        return this.queryById(reservation.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.reservationDao.deleteById(id) > 0;
    }

    //2.查询预约信息的方法(根据手机号查询)
    public Reservation findByPhone(String phone) {
        return reservationDao.findByPhone(phone);
    }
}
